<?php

/**
 * Description of Helpers_Showcase
 *
 * @author a.novikov
 */
class Helpers_Showcase extends Helpers_Base {

	public function getViewPath() {
		return 'layouts/blocks/showcase.tpl';
	}

	public function showcase($showcaseKey, $categoryId = 0, $count = 10) {
		$showcaseService = ShowcaseService::getInstance();
		$showcaseRow = $showcaseService->getShowcaseByKey($showcaseKey);

			$show_prod = ProductPeer::getInstance();
			$select = $show_prod->select();
			if($categoryId > 0){
				$select->join('product2product_category', '`product`.`id` = `product2product_category`.`product_id`', array());
				$select->where('`product2product_category`.`category_id` = ?', $categoryId);
			}
			$select->where($showcaseRow['filter'].' = 1');
			$select->where('`product`.`is_present` = 1');
			$select->where('`product`.`is_active` = 1');
			
			
			$cache = new Cache_Slot_ContentList('show_prod'.$showcaseKey.$categoryId, 3600);
			$result_show_prod = $cache->load();
			if (!$result_show_prod) {
				$result_show_prod = $show_prod->count($select);				
				$cache->save($result_show_prod);
			}		
		

			
			
			
			$this->view->count_prod_show = $result_show_prod;
		
		if($showcaseKey == 'recommend'){
				if ($showcaseRow) {
				$showcase = $showcaseService->getShowcase($showcaseRow, $categoryId);
				return $this->show($showcase, $count, 'recommend');
				}
		}else{
//		$showcaseRowset = ShowcasePeer::getInstance()->findByKey($showcaseKey);
//		if (!$showcaseRowset->count()) {
//			throw new InvalidArgumentException('Row does not exist');
//		}
//		$showcaseRow = $showcaseRowset->current();
			if ($showcaseRow) {
				$showcase = $showcaseService->getShowcase($showcaseRow, $categoryId);
				return $this->show($showcase, $count);
			}
		}
		return '';
	}

	protected function show(Showcase $showcase, $count = 10, $type_case = null) {

		$params = array(
			'showcase'	=> $showcase,
			'count'		=> $count
		);
		$this->view->showcase = List_Showcase_Random::factory($params, $type_case);
		return $this->render();
	}
}